package com.csyaonie.android.broadcast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.csyaonie.android.R;

public class BroadcastActivity1 extends AppCompatActivity {

    private Button b_btn1;
    private TextView b_tv1;
    private BroadcastReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast1);

        b_btn1=findViewById(R.id.b_btn1);
        b_tv1=findViewById(R.id.b_tv1);
        b_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(BroadcastActivity1.this,BroadcastActivity2.class);
                startActivity(intent);
            }
        });

        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("com.csyaonie.update");
        intentFilter.addAction("MY_BROADCAST");
        receiver=new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                switch (intent.getAction()){
                    case "com.csyaonie.update":
                        b_tv1.setText("yes broadcast success");
                        break;
                }
            }
        };
        LocalBroadcastManager.getInstance(this).registerReceiver(receiver,intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
    }
}